<?php

class HistoryController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	//public $layout='//layouts/column2';
	public $layout='column1';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','admin'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new History;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['History']))
		{
			$model->attributes=$_POST['History'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['History']))
		{
			$model->attributes=$_POST['History'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('History');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new History('search');
		$model->unsetAttributes();  // clear any default values
		
		if (isset($_GET['pageSize'])) {
			Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
			unset($_GET['pageSize']);
        }
		
		if(isset($_GET['History']))
			$model->attributes=$_GET['History'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=History::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='history-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	protected function checkUsername($data,$row)
	{
		$res = '';
		switch ($data->username) {
			case 'popova':
				$res = "Попова";
				break;
			case 'smirnova':
				$res = "Смирнова";
				break;				
			case 'platceva':
				$res = "Платцева";
				break;
			case 'jivova':
				$res = "Живова";
				break;
			case 'denisov':
				$res = "Денисов";
				break;
			case 'baraeva':
				$res = "Бараева";
				break;
			case 'fattahova':
				$res = "Фаттахова";
				break;	
			case 'vasilyeva':
				$res = "Васильева";
				break;	
			case 'andreev':
				$res = "Андреев";
				break;
			case 'voronina':
				$res = "Воронина";
				break;
			case 'gracheva':
				$res = "Грачева";
				break;
			case 'gasanov':
				$res = "Гасанов";
				break;
			case 'ejov':
				$res = "Ежов";
				break;
			case 'zabolot':
				$res = "Заболотная";
				break;
			case 'kalachov':
				$res = "Калачов";
				break;
			case 'mainstrenko':
				$res = "Майстренко";
				break;
			case 'fomichev':
				$res = "Фомичев";
				break;				
			case 'novikov':
				$res = "Новиков";
				break;
			case 'chanov':
				$res = "Чанов";
				break;	
			case 'zasslavskiy':
				$res = "Засславский";
				break;				
			case 'pavluk':
				$res = "Павлюк";
				break;
			case 'simakov':
				$res = "Симаков";
				break;	
			case 'sales':
				$res = "Попова";
				break;	
			case 'simonov':
				echo "Симонов";
				break;
			case 'kishov':
				echo "Кишов";
				break;
			case 'samkova':
				echo "Самкова";
				break;				
		}
		return $res;
	}
	protected function checkModel($data,$row)
	{
		$res = '';
		switch ($data->model) {
			case 'Promo':
				$res = "Реклама";
				break;
			case 'Editors':
				$res = "Редакция";
				break;
			case 'Markups':
				$res = "Верстка";
				break;
		}
		return $res;
	}	
	protected function checkСonditions($data,$row)
	{
		$res = '';
		switch ($data->conditions) {
			case 'pend':
				$res = "Ожидается";
				break;
			case 'get':
				$res = "Получено";
				break;
			case 'gmark':
				$res = "Отправлено на верстку";
				break;
			case 'gedit':
				$res = "Отправлено в редакцию";
				break;
			case 'gsogl':
				$res = "Отправлено на согласование";
				break;
			case 'gadmit':
				$res = "Отправлено на утверждение";
				break;
			case 'sogl':
				$res = "Согласование";
				break;
			case 'mark':
				$res = "Верстка";
				break;
			case 'admit':
				$res = "Утверждение";
				break;
			case 'success':
				$res = "Утверждено";
				break;
			case 'work':
				$res = "В работе";
				break;
			case 'sended':
				$res = "Отправлено";
				break;				
		}
		return $res;
	}
	protected function checkСommedit($data,$row)
	{
		$res = '';
		switch ($data->commedit) {
			case 'ok':
				$res = "Принято";
				break;
			case 'almost':
				$res = "Принято с замечаниями";
				break;
			case 'fail':
				$res = "Не принято";
				break;
		}
		return $res;
	}
	protected function checkСommmark($data,$row)
	{
		$res = '';
		switch ($data->commmark) {
			case 'ok':
				$res = "Принято";
				break;
			case 'almost':
				$res = "Принято с замечаниями";
				break;
			case 'fail':
				$res = "Не принято";
				break;
		}
		return $res;
	}	
	protected function checkСomments($data,$row)
	{
		$res = '';
		switch ($data->comments) {
			case 'ok':
				$res = "Принято";
				break;
			case 'almost':
				$res = "Принято с замечаниями";
				break;
			case 'fail':
				$res = "Не принято";
				break;
		}
		return $res;
	}	
}
